﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MacViet.DataBaseAPI;


[AjaxPro.AjaxNamespace("Ajax_Control_Appointment_app")]
public partial class UCAddAppoint : System.Web.UI.UserControl
{
    private static int acc_id = 0;
    private static DateTime date;
    private APPOINTMENTSDataAPI app = new APPOINTMENTSDataAPI();
    private SERVICE_CATEGROIESDataAPI cate = new SERVICE_CATEGROIESDataAPI();
    private SERVICESDataAPI ser = new SERVICESDataAPI();
    private EMPLOYEESDataAPI EMP = new EMPLOYEESDataAPI();
    private SPADataAPI spa = new SPADataAPI();
    private CLIENTSDataAPI cl = new CLIENTSDataAPI();
    public DataTable dtResult = new DataTable();
    private MyResult result = new MyResult();
    public DataTable dtRes = new DataTable();
    private string acc_email = string.Empty;
    PRODUCTSDataAPI product = new PRODUCTSDataAPI();
    APPOINTMENTSDataAPI appmore = new APPOINTMENTSDataAPI();
    BILLDataAPI BILL = new BILLDataAPI();
    
    

    protected void Page_Load(object sender, EventArgs e)
    {        
        
        //btnSave.Attributes.Add("OnClick", "return SaveClick(this);");
        if (!IsPostBack)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(UCAddAppoint));
            if (Session["acc_id"] != null)
                acc_id = int.Parse(Session["acc_id"].ToString());
            if (Request.QueryString["date"] != null && Request.QueryString["date"].ToString() != string.Empty)
                date = DateTime.Parse(Request.QueryString["date"].ToString());
            txtDate.Text = date.ToShortDateString();
            LoadClient();
            LoadEmployees();
            LoadSpa();
            loadService2(acc_id);
        }        
        
    }

    private void LoadClient()
    {
        try
        {
            dtResult = cl.GetAllClient(acc_id);
            ddlClient.DataSource = dtResult;
            ddlClient.DataTextField = "CLI_NAME";
            ddlClient.DataValueField = "CLI_ID";
            ddlClient.DataBind();
            ddlClient.Items.Insert(0, new ListItem("-Choose Client Name-", "0"));
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }
    private void LoadEmployees()
    {
        try
        {
            dtResult = EMP.getAllEmployees(acc_id);
            ddlEmp.DataSource = dtResult;
            ddlEmp.DataTextField = "EMP_NAME";
            ddlEmp.DataValueField = "EMP_ID";
            ddlEmp.DataBind();
            ddlEmp.Items.Insert(0, new ListItem("-Choose Employees Name-", "0"));
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }
    private void LoadSpa()
    {
        try
        {
            dtResult = spa.getAllSpa(acc_id);
            ddlSpa.DataSource = dtResult;
            ddlSpa.DataTextField = "SPA_NAME";
            ddlSpa.DataValueField = "SPA_ID";
            ddlSpa.DataBind();
            ddlSpa.Items.Insert(0, new ListItem("-Choose SPA -", "0"));
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }
    [AjaxPro.AjaxMethod]
    public string[] addNewClient(string name, string email, string phone)
    {
        int newId = cl.insertClientOutID(acc_id, name, email, phone); 
        return new string[] {newId + "", name };
    }
    private void loadService2(int acc_id)
    {
        dtResult = cate.getByACC_ID(acc_id);
        GrvServicePoPuP.DataSource = dtResult;
        GrvServicePoPuP.DataBind();
    }
    protected void GrvServicePoPuP_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddlServiceTypePoPuP = e.Row.FindControl("ddlServiceTypePoPuP") as DropDownList;
            DropDownList ddlServicePoPuP = e.Row.FindControl("ddlServicePoPuP") as DropDownList;
            Label lblPriceServicePoPuP = e.Row.FindControl("lblPriceServicePoPuP") as Label;

            dtRes = cate.getByACC_ID(acc_id);
            ddlServiceTypePoPuP.DataSource = dtRes;
            ddlServiceTypePoPuP.DataTextField = "SERCAT_NAME";
            ddlServiceTypePoPuP.DataValueField = "SERCAT_ID";
            ddlServiceTypePoPuP.DataBind();

            ddlServiceTypePoPuP.Items.Insert(0, new ListItem("-Choose Item-", "0"));

            ddlServiceTypePoPuP.Attributes.Add("onChange", "return ServiceTypeChange('" + ddlServicePoPuP.ClientID + "',this);");
            ddlServicePoPuP.Attributes.Add("onChange", "return ServiceChange('" + ddlServicePoPuP.ClientID + "','" + lblPriceServicePoPuP.ClientID + "',this);");


        }
    }
    [AjaxPro.AjaxMethod]
    public MyResult loadServiceBySerTypeId(string value, string SerID)
    {
        MyResult My = new MyResult();
        DataTable dtser = ser.getBySER_ID(Convert.ToInt32(value), acc_id);
        My.tbl = dtser;
        My.SerID = SerID;
        return My;

        //dt = cate.getBySERCATE_ID(Convert.ToInt32(id));
        //return dt;
    }
    [AjaxPro.AjaxMethod]
    public MyResult LoadServicePrice(string val, string PriceID)
    {
        MyResult result = new MyResult();
        DataTable get = ser.getServices_Price(Convert.ToInt32(val), acc_id);
        result.tbl = get;
        result.PriceID = PriceID;
        return result;
    }
    [AjaxPro.AjaxMethod]
    public string[] InsertDataPopUp(string emp_id, string cli_id, string spa_id, string getSerName, string getSerPrice, string getserid,string Time)
    {
            //int IDSer = 1;
            //DataTable topsER = BILL.getTopBIll_Ser_Detail(acc_id);
            //if (topsER != null)
            //    IDSer = int.Parse(topsER.Rows[0]["BILL_SER_ID"].ToString()) + 1;            
            string TimeGet = Time.ToString();            
            int EmpId = int.Parse(emp_id);
            int CliID = int.Parse(cli_id);
            int SpaId = int.Parse(spa_id);
            string[] strSerName = getSerName.Split(',');
            string[] strSerPrice = getSerPrice.Split(',');
            string[] strServiceID = getserid.Split(',');
            string appstatus = "WAIT";
            int AppId = appmore.InsertAppointmentOutID(EmpId, CliID, SpaId, date, TimeGet, acc_id);
            APPOINTMENTSDataAPI apphet = new APPOINTMENTSDataAPI();
            for (int i = 0; i < strSerPrice.Length; i++)
            {
                int SerViceId = int.Parse(strServiceID[i].ToString());
                string ServiceName = strSerName[i].ToString();
                double ServicePrice = double.Parse(strSerPrice[i].ToString());   
                appmore.insertAPPOINTMENT_SERVICES(AppId, SerViceId, appstatus);
            }

            string re = AppId.ToString();
            return new string[] {re};
    }
    protected void btnExit_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Appointment.aspx");
    }
}
